package com.news.news.web;

import com.news.news.modle.User;
import com.news.news.service.UserWebService;
import com.news.news.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户注册页面
 */
@RestController
@RequestMapping(path = "/userApi/userCtl")
public class UserWebController {
    @Autowired
    UserWebService userWebService;


    /**
     * 生成验证码
     * @return
     */
        @GetMapping(path = "/createYzm")
    public CommonResult createYzm(String account){
      //  查询注册账号是否存在
      User user =   userWebService.findUserByAccount(account);
        CommonResult commonResult;
        if (user==null){
          userWebService.createYzm(account);
          commonResult = new CommonResult(200,null,"验证码生成成功");
        }else {
            commonResult = new CommonResult(203,null,"账号已存在！");
        }
        return commonResult;
    }


    /*
      注册保存用户
     */
    @PostMapping(path = "/save")
    public CommonResult save(@RequestBody User user){
        int res =  userWebService.save(user);
        System.out.println("保存成功");
        return new CommonResult(200, res,"查询成功");
    }

    /*
      注册保存用户
     */
    @PostMapping(path = "/userLogin")
    public CommonResult userLogin(@RequestBody User user){
        User u =  userWebService.userLogin(user);
        return new CommonResult(200, u,"查询成功");
    }


    //忘记密码
    @GetMapping(path = "/sendPsw")
    public CommonResult sendPsw(String account,String name){
       String msg =  userWebService.sendPsw(account,name);
        return new CommonResult(200,null,msg);
    }
}
